//
//  LoginVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/12.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "ForgetPwdVC.h"
#import "LoginModel.h"
#import "MyCenterVC.h"
#import "LRTranstionAnimationPush.h"

@interface LoginVC ()<UINavigationControllerDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *emailTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *signinBtn;
@property (weak, nonatomic) IBOutlet UIButton *signUpBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdBtn;

@property (weak, nonatomic) IBOutlet UIButton *emaillBtn;
@property (weak, nonatomic) IBOutlet UIButton *pwdlBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emaillBtn_w;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pwdlBtn_w;



@property (nonatomic, retain) RefreshVM *vm;


@end

@implementation LoginVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    
    self.emailTF.text = @"happybean6@163.com";
    self.pwdTF.text = @"123123";
    
    [self setTFPlaceHolderTextColor:self.emailTF];
    [self setTFPlaceHolderTextColor:self.pwdTF];
    
    
}

-(void)setTFPlaceHolderTextColor:(UITextField *)tf{
    [tf setValue:DEF_0xColor(0x8F98E2, 1) forKeyPath:@"_placeholderLabel.textColor"];

}

- (IBAction)signInBtnClick:(UIButton *)sender {
    if (self.emailTF.text.length < 1) {
        [Utils showHudWithTitle:@"请输入账号" delay:1.0 target:self];
        return;
    }
    if (self.pwdTF.text.length < 1) {
        [Utils showHudWithTitle:@"请输入密码" delay:1.0 target:self];
        return;
    }
    self.button=sender;
    [self configViewModel];
}
- (IBAction)signUpBtnClick:(UIButton *)sender {
    RegisterVC *vc = [RegisterVC new];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)forgetPwdBtnClick:(UIButton *)sender {
    ForgetPwdVC *vc = [ForgetPwdVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)configViewModel{
    weakself
    self.vm = [RefreshVM new];
    self.vm.urlStr = @"/api/authorizations";
    self.vm.params = [self configParam];
    self.vm.dataClass = [LoginModel class];
    [self.vm requestDataNormal];
    [self.vm setRequestResultBlock:^(id obj) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        LoginModel *model = obj;
        [user setObject:model.access_token forKey:@"access_token"];
        MyCenterVC *vc = [MyCenterVC new];
        [self.navigationController pushViewController:vc animated:YES];
//        [ws dismissViewControllerAnimated:YES completion:^{
//
//        }];

        NSLog(@"%ld", (long)model.expires_in);
        
    }];
}

-(NSDictionary *)configParam{
    return @{@"account": self.emailTF.text, @"password": self.pwdTF.text};
}

-(CAKeyframeAnimation *)getAnimation:(CGRect)frame{
    //沿着中心左移5上移5
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    positionAnimation.calculationMode = kCAAnimationPaced;
    positionAnimation.fillMode = kCAFillModeForwards;
    positionAnimation.repeatCount = MAXFLOAT;
    //        positionAnimation.autoreverses = YES;
    //        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    positionAnimation.duration = 1;
    
    UIBezierPath *positionPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(frame, frame.size.width/2, frame.size.height/2-15)];
    positionAnimation.path = positionPath.CGPath;
//    [button.layer addAnimation:positionAnimation forKey:nil];
    return positionAnimation;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.emailTF) {
        self.emaillBtn_w.constant = 14;
        [self.emaillBtn setBackgroundImage:IMAGE(@"") forState:UIControlStateNormal];
        self.emaillBtn.backgroundColor = DEF_0xColor(0x00D8FF, 1);
//        [self.emaillBtn.layer addAnimation:[self getAnimation:self.emaillBtn.frame] forKey:nil];
//        self.emaillBtn.hidden = NO;
        

    }else{
        self.pwdlBtn_w.constant = 14;
        [self.pwdlBtn setBackgroundImage:IMAGE(@"") forState:UIControlStateNormal];
        self.pwdlBtn.backgroundColor = DEF_0xColor(0x00D8FF, 1);
//        [self.pwdlBtn.layer addAnimation:[self getAnimation:self.pwdlBtn.frame] forKey:nil];
//        self.pwdlBtn.hidden = NO;

    }
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.emailTF) {
//        self.emaillBtn_w.constant = 14;
//        [self.emaillBtn setBackgroundImage:IMAGE(@"") forState:UIControlStateNormal];
//        self.emaillBtn.backgroundColor = DEF_0xColor(0x00D8FF, 1);
        [self.emaillBtn.layer addAnimation:[self getAnimation:self.emaillBtn.frame] forKey:nil];
        self.emaillBtn.hidden = NO;
        
        
    }else{
//        self.pwdlBtn_w.constant = 14;
//        [self.pwdlBtn setBackgroundImage:IMAGE(@"") forState:UIControlStateNormal];
//        self.pwdlBtn.backgroundColor = DEF_0xColor(0x00D8FF, 1);
        [self.pwdlBtn.layer addAnimation:[self getAnimation:self.pwdlBtn.frame] forKey:nil];
        self.pwdlBtn.hidden = NO;
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.emailTF) {
        [self.emaillBtn.layer removeAllAnimations];
        if (textField.text.length < 1) {
            self.emaillBtn.hidden = YES;
        }else{
            [self.emaillBtn setBackgroundImage:IMAGE(@"gou") forState:UIControlStateNormal];
            self.emaillBtn_w.constant = 30;
            self.emaillBtn.backgroundColor = [UIColor clearColor];
            [UIView animateWithDuration:0.4 animations:^{
                [self.view setNeedsLayout];
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
            }];
        }
    }else{
        [self.pwdlBtn.layer removeAllAnimations];
        if (textField.text.length < 1) {
            self.pwdlBtn.hidden = YES;
            
        }else{
            [self.pwdlBtn setBackgroundImage:IMAGE(@"gou") forState:UIControlStateNormal];
            self.pwdlBtn_w.constant = 30;
            self.pwdlBtn.backgroundColor = [UIColor clearColor];
            [UIView animateWithDuration:0.4 animations:^{
                [self.view setNeedsLayout];
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                
            }];
        }
    }
}

#pragma mark -- UINavigationControllerDelegate --

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    
    if (operation == UINavigationControllerOperationPush) {
        return [LRTranstionAnimationPush new];
    }else{
        return nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
